package com.dqp.cslggroup.ZCJXJ;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.SmartTableInfo.dakaInfo;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.JxjAdapter;
import com.dqp.cslggroup.UI.MyListView;
import com.dqp.cslggroup.UI.twoTextViewAdapter;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.bean.Jxj;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class XSGZ_main extends BaseActivity {
    private String zc, jxj;
    private MyListView listView, listView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zc_jxj);
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        zc = intent.getStringExtra("zc");
        jxj = intent.getStringExtra("jxj");
        initCmp();
        initZcView();
        initJxjView();
    }

    private void initCmp() {
        listView = findViewById(R.id.zc_list);
        listView2 = findViewById(R.id.jxj_list);
        TextView ksmc, kssj, cdmc, kcmc;
        ksmc = findViewById(R.id.text_xf);
        kssj = findViewById(R.id.text_cj);
        cdmc = findViewById(R.id.text_jd);
        kcmc = findViewById(R.id.text_kc);
        kcmc.setText("姓名");
        ksmc.setText("学年");
        cdmc.setText("综测");
        kssj.setText("学期");
    }

    public void initZcView() {
        ArrayList<Jxj> dataList = new ArrayList<>();
        if (zc != null && zc.length() > 0) {
            zc = zc.replace("班级编号  入学年份", "");
            String[] strArr;
            String[] arr;
            arr = zc.trim().split("查看");
            for (String s : arr) {
                strArr = s.split(" ");
                if (strArr[0].isEmpty()) {
                    Jxj jxj = new Jxj();
                    jxj.setXm(strArr[1]);
                    jxj.setXn(strArr[3]);
                    jxj.setXq(strArr[4]);
                    jxj.setZc(strArr[5]);
                    dataList.add(jxj);
                } else {
                    Jxj jxj = new Jxj();
                    jxj.setXm(strArr[0]);
                    jxj.setXn(strArr[2]);
                    jxj.setXq(strArr[3]);
                    jxj.setZc(strArr[4]);
                    dataList.add(jxj);
                }
            }
            JxjAdapter jxjAdapter = new JxjAdapter(dataList, XSGZ_main.this);
            listView.setAdapter(jxjAdapter);
            listView.setOnItemClickListener((adapterView, view, i, l) -> {
                final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(XSGZ_main.this);
                view = LayoutInflater.from(XSGZ_main.this).inflate(R.layout.jxj_more, null);
                builder.setView(view);
                TextView title = view.findViewById(R.id.title);
                title.setText("综测详情");
                ListView listView = view.findViewById(R.id.list);
                Dialog dialog = builder.show();
                UiUtils.setDialogWindowAttr(dialog, XSGZ_main.this);
                Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
                String[] zc_text = new String[]{"姓名：\t", "学号：\t", "学年：\t", "学期：\t", "综测成绩：\t",
                        "班级排名：\t", "学习成绩：\t", "两课成绩：\t", "体育成绩：\t", "双学小组：\t",
                        "社会实践：\t", "班团活动：\t", "宿舍文明：\t", "行为修养：\t", "义工活动：\t",
                        "上课出勤：\t", "经典阅读：\t", "体育竞赛：\t", "智育成绩：\t", "德育成绩：\t",
                        "体育成绩：\t", "加分：\t", "减分：\t"};
                List<dakaInfo> list = new ArrayList<>();
                if (zc != null && zc.length() > 0) {
                    zc = zc.replace("班级编号  入学年份", "");
                    String[] strArr1, arr1;
                    StringBuilder text = new StringBuilder();
                    arr1 = zc.trim().split("查看");
                    strArr1 = arr1[i].split(" ");
                    if (strArr1[0].isEmpty()) {
                        for (int x = 0; x < 23; x++) {
                            list.add(new dakaInfo(zc_text[x], strArr1[x + 1]));
                            text.append(zc_text[x]).append(strArr1[x + 1]).append("\n");
                        }
                    } else {
                        for (int x = 0; x < 23; x++) {
                            list.add(new dakaInfo(zc_text[x], strArr1[x]));
                            text.append(zc_text[x]).append(strArr1[x]).append("\n");
                        }
                    }
                    twoTextViewAdapter adapter = new twoTextViewAdapter(list, this);
                    listView.setAdapter(adapter);
                }
            });
        }
    }

    public void initJxjView() {
        ArrayList<Jxj> dataList = new ArrayList<>();
        if (jxj != null && jxj.length() > 0) {
            String[] strArr;
            String[] arr;
            arr = jxj.trim().split("金 ");
            for (String s : arr) {
                strArr = s.split(" ");
                if (strArr[0].isEmpty()) {
                    Jxj jxj = new Jxj();
                    jxj.setXm(strArr[1]);
                    jxj.setXn(strArr[3]);
                    jxj.setXq(strArr[4]);
                    if (strArr[5].contains("金"))
                        jxj.setZc(strArr[5]);
                    else jxj.setZc(strArr[5] + "金");
                    dataList.add(jxj);
                } else {
                    Jxj jxj = new Jxj();
                    jxj.setXm(strArr[0]);
                    jxj.setXn(strArr[2]);
                    jxj.setXq(strArr[3]);
                    if (strArr[4].contains("金"))
                        jxj.setZc(strArr[4]);
                    else jxj.setZc(strArr[4] + "金");
                    dataList.add(jxj);
                }
            }
            JxjAdapter jxjAdapter = new JxjAdapter(dataList, XSGZ_main.this);
            listView2.setAdapter(jxjAdapter);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
